<?php
	function createView($view){		
		
	   $current_user = wp_get_current_user(); 
	   if($current_user->ID == 0){
	   		return '';
	   }
	   
	   $simple = true;
	   if($view->table == "custom"){
	   		$simple = false;
	   }

	   include_once "functions.php";
	   $user = UsersORM::getUser(true);

	   /*
	   echo json_encode($user);

	   echo json_encode($view->user);
	   echo "<br>";

	   echo $view->findAllQuery();
	   */

	   //echo  $view->granted;
	   //echo json_encode($view);
	   
	   /*if($view->granted != 1){
	   		die();
	   }*/
	   
	   

?>

<div>
	
	<?php if( $view->class =="clientcourseView"){	?>
	<div>
		<form  data-bind="submit:clickSearch">
			<div id="the-basics">
			  <input class="typeahead" type="text"  data-bind="value:searchString">
			  <button type="button" name="Поиск" data-bind="click:clickReset">Сброс</button>
			  <button type="button" name="Поиск" data-bind="click:clickSearch">Поиск</button>
			</div>

			
		</form>
	</div>
	<?php }	 ?>

	<table class="timeTable minpadding">
		<caption> <?php echo $view->name; ?> </caption>
		<thead>
			<tr>
				<th class="center">N</th>

				<th class="edit" data-bind="click:views.<?php echo $view->class;?>().clickNew" colspan="3"><span class="glyphicon glyphicon-plus"></th>
				<?php 
					foreach ($view->header as $key => $value) {
						if()
				?>
						<th class="center minpadding"> <?php echo $value["name"]; ?>  </th>
				<?php  } ?>	
			</tr>
		</thead>
		<tbody data-bind="foreach: views.<?php echo $view->class;?>().model.body">
			<tr>
			   <!-- ko ifnot: state.edit-->
			   	<td class="center minpadding" data-bind="text:row_num, click:events.clickCell"></td>
			   	
			   	<td class="edit" data-bind="click:events.clickCell" colspan="3"><span class="glyphicon glyphicon-pencil"></span> </td>
			   	

				<?php foreach ($view->header as $key => $value) { ?>
					
				<?php	if ($value["format"] == "c" ) { ?>
						<td class ="minpadding squareTableTD" data-bind="squareTable:{count:items().<?php echo $value["fields"][0] ;?> , total:items().<?php echo $value["fields"][1] ;?>}"></td>


				<?php	}else{ ?>
						<td class="center minpadding" data-bind="text:items().<?php echo $key; ?>"></td>
				<?php	}	?>			


				<?php  } ?>	
				<!-- /ko -->
				<!-- ko if: state.edit-->
				<td data-bind="text:row_num,click:events.clickCell"></td>
			   	<td class="edit" data-bind="click:events.clickCell"><span class="glyphicon glyphicon-share-alt"></span> </td>
				<td> <span class="glyphicon glyphicon-ok" data-bind="click:events.clickCellSave" ></span> </td>
				<td><span class="glyphicon glyphicon-remove" data-bind="click:events.clickCellDelete"></span></td>
				
				<?php foreach ($view->header as $key => $value) { 
						 if( $value["format"] == "s" || $value["format"] == "d"){
				?>	
							<td> <input type="text" data-bind="value:items().<?php echo $key; ?>"></td>
				<?php   
						} elseif ($value["format"] == "select") {
				?>			
							<td> 
								<select data-bind="options:selects.<?php echo $value["select_name"];?>.values, optionsText:'field', value:selects.<?php echo $value["select_name"];?>.value, event:{change:events.selected}  "></select>
							</td>					
				<?php   
						} elseif ($value["format"] == "date") {
				?>			
							<td> 
								<input data-bind="datepicker:items().<?php echo $key; ?>"></select>
							</td>						
				<?php   
						} elseif ($value["format"] == "c") {
				?>			
							<td class="squareTableTD" data-bind="squareTable:{count:items().<?php echo $value["fields"][0] ;?> , total:items().<?php echo $value["fields"][1] ;?>}"></td>							
				<?php	}
				    } 
				?>	
				<!-- /ko -->
			</tr>		
			<?php if(!$simple){ ?>
			<tr>	
			
	
			</tr>
			<?php } ?>
		</tbody>
	</table>
</div>

<hr>
<script>
	var tmp_string = <?php  echo json_encode( $view->getView() ); ?>;
	var _DATAVIEWS = _DATAVIEWS || {};
	_DATAVIEWS['<?php echo $view->class; ?>'] = tmp_string;
	console.log(_DATAVIEWS);
</script>


<?php }?>